import { GameObject } from "./GameObject";
import { Snake } from "./Snake";
import { Wall } from "./Wall";
export class GameMap extends GameObject {
    constructor(ctx, parent, store) {
        super();
        this.ctx = ctx;
        this.parent = parent;
        this.store = store,

            this.L = 0; //每格标准单位
        this.row = 13;
        this.col = 14;
        this.inner_walls_count = 20;
        this.walls = []

        this.snakes = [
            new Snake({ id: 0, color: "#AA4942", r: this.row - 2, c: 1 }, this),
            new Snake({ id: 1, color: "#7B9D99", r: 1, c: this.col - 2 }, this)
        ]
    }



    start() {
        this.create_walls();
        this.add_listening_event()
    }

    //给cavans绑定事件
    add_listening_event() {
        if (this.store.state.record.is_record) {
            let k = 0;
            const a_step = this.store.state.record.a_step;
            const b_step = this.store.state.record.b_step;
            const record_loser = this.store.state.record.record_loser;
            const [snake0, snake1] = this.snakes;

            const interval_id = setInterval(() => {
                if (k >= a_step.length - 1) {
                    if (record_loser === 'all') {
                        snake0.status = "die";
                        snake1.status = "die";
                    } else if (record_loser === 'A') {
                        snake0.status = "die";
                    } else {
                        snake1.status = "die";
                    }
                    clearInterval(interval_id);
                } else {
                    snake0.set_derection(parseInt(a_step[k]));
                    snake1.set_derection(parseInt(b_step[k]));
                    k++;
                }
            }, 300)
        } else {
            this.ctx.canvas.focus();
            this.ctx.canvas.addEventListener("keydown", e => {
                let d = -1
                if (e.key === 'w') d = 0;
                else if (e.key === 'd') d = 1;
                else if (e.key === 's') d = 2;
                else if (e.key === 'a') d = 3;

                if (d >= 0) {
                    this.store.state.pk.socket.send(JSON.stringify({
                        event: "move",
                        direction: d,
                    }))
                }
            })
        }

    }
    //判断两蛇是否要走
    check_ready() {
        for (const snake of this.snakes) {
            if (snake.status !== "stop") return false
            if (snake.drection === -1) return false;
        }
        return true;
    }

    //让两蛇进入下一回合
    next_step() {
        for (const snake of this.snakes) {
            snake.next_step();
        }
    }

    //检查目标位置是否合法
    check_vaild(cell) {
        for (const wall of this.walls)
            if (wall.r === cell.r && wall.c === cell.c) return false;

        for (const snake of this.snakes) {
            let k = snake.cells.length;
            //蛇尾能缩短别判断了；
            if (!snake.check_length_add()) {
                k--;
            }
            for (let i = 0; i < k; i++) {
                const c = snake.cells[i];
                if (c.r === cell.r && c.c === cell.c) return false;
            }
        }

        return true;

    }

    //制造墙
    create_walls() {
        const g = this.store.state.pk.gamemap;
        for (let r = 0; r < this.row; r++) {
            for (let c = 0; c < this.col; c++) {
                if (g[r][c] === 1) {
                    this.walls.push(new Wall(r, c, this));
                }
            }
        }

        return true;

    }

    //画画板
    update_size() {
        this.L = parseInt(Math.min(this.parent.clientWidth / this.col, this.parent.clientHeight / this.row))
        this.ctx.canvas.width = this.L * this.col;
        this.ctx.canvas.height = this.L * this.row;
    }

    //每一帧执行的操作
    update() {
        this.update_size()
        if (this.check_ready()) {
            this.next_step();
        }
        this.render()
    }

    //渲染
    render() {
        const color_new = "#AAD751", color_odd = "#A2D149";

        for (let y = 0; y < this.row; y++) {
            for (let x = 0; x < this.col; x++) {
                if ((x + y) % 2 == 0) {
                    this.ctx.fillStyle = color_new;
                } else {
                    this.ctx.fillStyle = color_odd;
                }
                this.ctx.fillRect(x * this.L, y * this.L, this.L, this.L);
            }
        }
    }

}